import pygame
import sys
import random

# 初始化pygame
pygame.init()

# 游戏窗口大小
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 20

# 颜色定义
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)

# 设置窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇游戏')
clock = pygame.time.Clock()

# 字体
font = pygame.font.SysFont('SimHei', 24)

def draw_snake(snake):
    for segment in snake:
        pygame.draw.rect(screen, GREEN, (*segment, CELL_SIZE, CELL_SIZE))

def draw_food(position):
    pygame.draw.rect(screen, RED, (*position, CELL_SIZE, CELL_SIZE))

def random_food(snake):
    while True:
        x = random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
        y = random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
        if (x, y) not in snake:
            return (x, y)

def show_score(score):
    score_surface = font.render(f'得分: {score}', True, BLACK)
    screen.blit(score_surface, (10, 10))

def main():
    snake = [(100, 100), (80, 100), (60, 100)]
    direction = 'RIGHT'
    change_to = direction
    food = random_food(snake)
    score = 0
    speed = 10

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and direction != 'DOWN':
                    change_to = 'UP'
                elif event.key == pygame.K_DOWN and direction != 'UP':
                    change_to = 'DOWN'
                elif event.key == pygame.K_LEFT and direction != 'RIGHT':
                    change_to = 'LEFT'
                elif event.key == pygame.K_RIGHT and direction != 'LEFT':
                    change_to = 'RIGHT'

        direction = change_to
        x, y = snake[0]
        if direction == 'UP':
            y -= CELL_SIZE
        elif direction == 'DOWN':
            y += CELL_SIZE
        elif direction == 'LEFT':
            x -= CELL_SIZE
        elif direction == 'RIGHT':
            x += CELL_SIZE
        new_head = (x, y)

        # 检查碰撞
        if (
            x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT or new_head in snake
        ):
            break  # 游戏结束

        snake.insert(0, new_head)
        if new_head == food:
            score += 1
            food = random_food(snake)
            speed = min(25, speed + 0.5)
        else:
            snake.pop()

        screen.fill(WHITE)
        draw_snake(snake)
        draw_food(food)
        show_score(score)
        pygame.display.flip()
        clock.tick(speed)

    # 游戏结束显示
    screen.fill(WHITE)
    game_over_surface = font.render('游戏结束! 按任意键退出', True, RED)
    score_surface = font.render(f'最终得分: {score}', True, BLACK)
    screen.blit(game_over_surface, (WIDTH // 2 - 120, HEIGHT // 2 - 30))
    screen.blit(score_surface, (WIDTH // 2 - 60, HEIGHT // 2 + 10))
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main() 